"""
@Author  : JACK
@File    : 33.py
@Time    : 2020/4/30 14:33
@Software: PyCharm
text 单接口测试用例报告模板
"""
from jinja2 import PackageLoader, Environment


# 报告生成方法
def report_html(bodys, rest):
    # 默认去lib目录下templates文件夹下html文件
    env = Environment(loader=PackageLoader(package_name="lib"))  # 创建一个包加载器对象
    template = env.get_template('bast.html')  # 获取一个模板文件
    with open("results.html", "w+", encoding="utf-8") as f:
        html_content = template.render(bodys=bodys, rest=rest)
        f.write(html_content)
        f.close()


if __name__ == "__main__":
    # 示例
    import datetime

    body = []
    result = {'caseID': 3, 'casename': '登陆', 'url': 'https://www.baidu.com', 'request_conunt': '详情',
              'method': 'post_json', 'input_args': '{\n    "name": "jack",\n    "passWord": "123456"\n}',
              'rest': '{\n    "status": {\n        "statusCode": 0,\n        "statusReason": ""\n    },\n    "result": {\n        "code": 200,\n        "message": "success",\n        "merchantId": "12591",\n            "registerStatus": 1,\n       "verCode": null\n    }\n}',
              'results': 'PASS'}
    body.append(result)

    rest = {"start_time": "2020-03-26 14:11:45.683299", "end_time": "2020-03-26 14:11:45.683299",
            "pro_report": "接口测试报告名称",
            "time_ls": "14:11:45.683299", "All": "2", "Pass": "1", "Fail": "1", "Error": "1", "data": body}
    bodys = rest["data"]
    print(bodys)
    report_html(bodys=bodys, rest=rest)
